#pragma once

/* 页定义 */
#define PAGE_SHIFT			(12UL)
#define PAGE_SIZE			(1UL << PAGE_SHIFT)
#define PAGE_MASK			(~(PAGE_SIZE - 1))

#define HPAGE_SIZE(lvl)			HPAGE_SIZE_##lvl
#define HPAGE_SIZE_0			PAGE_SIZE
#define HPAGE_SIZE_1			(1UL << (PAGE_SHIFT + 9))
#define HPAGE_SIZE_2			(1UL << (PAGE_SHIFT + 9 + 9))
#define HPAGE_SIZE_3			(1UL << (PAGE_SHIFT + 9 + 9 + 9))

#define PTR_ARITH(ptr, op, offst)	((void *)((paddr_t)(ptr) op (offst)))

#define BYTE_WIDTH			__CHAR_BIT__


/* 后期可以用编译配置消除这个文件 */
/* ARCH USING GCC */
#if defined(__amd64__)
	#define WORD_SIZE		8
	#define HDEF_ARCH_X86
	#define HDEF_ARCH_X86_64
	#define HDEF_CACHE_LINE_BYTES	64
	#define HDEF_PTR_BITS		48
	#define HDEF_STK_GROWS_DOWNWARD	1
	#define HDEF_STK_ALIGN_BYTES	16
#elif   defined(__i386__)
	#define WORD_SIZE		4
	#define HDEF_ARCH_X86
	#define HDEF_ARCH_X86_32
	#define HDEF_CACHE_LINE_BYTES	64
	#define HDEF_PTR_BITS		32
	#define HDEF_STK_GROWS_DOWNWARD	1
	#define HDEF_STK_ALIGN_BYTES	16
#elif   defined(__arm__)
	#define WORD_SIZE		4
	#define HDEF_ARCH_ARM
	#define HDEF_ARCH_ARM_32
	#define HDEF_CACHE_LINE_BYTES	64
	#define HDEF_PTR_BITS 		32
	#define HDEF_STK_GROWS_DOWNWARD	1
	#define HDEF_STK_ALIGN_BYTES	16
	#define HDEF_WEAK_MEMORY_ORDER
#elif   defined(__arm64__) || defined(__aarch64__)
	#define WORD_SIZE		8
	#define HDEF_ARCH_ARM
	#define HDEF_ARCH_ARM_64
	#define HDEF_ARCH_AARCH_64
	#define HDEF_CACHE_LINE_BYTES	64
	#define HDEF_PTR_BITS		48
	#define HDEF_STK_GROWS_DOWNWARD	1
	#define HDEF_STK_ALIGN_BYTES	32
	#define HDEF_WEAK_MEMORY_ORDER
#else
	#error "unsupported architecture"
#endif

#define PTR_MASK			((1UL << HDEF_PTR_BITS) - 1)


#define __WORDEVAL(a, word)		a##word
#define _WORDEVAL(a, word)		__WORDEVAL(a, word)


#define DWORD_WIDTH			_WORDEVAL(__DWORD_BIT, WORD_SIZE)
#define DWORD_SIZE			_WORDEVAL(__DWORD_SIZE, WORD_SIZE)
#define WORD_WIDTH			_WORDEVAL(__WORD_WIDTH, WORD_SIZE)
#define HWORD_SIZE			_WORDEVAL(__HWORD_SIZE, WORD_SIZE)
#define HWORD_WIDTH			_WORDEVAL(__HWORD_WIDTH, WORD_SIZE)

#define __DWORD_BIT8			128
#define __DWORD_SIZE8			16
#define __WORD_WIDTH8			64
#define __HWORD_SIZE8			4
#define __HWORD_WIDTH8			32

#define __DWORD_BIT4			64
#define __DWORD_SIZE4			8
#define __WORD_WIDTH4			32
#define __HWORD_SIZE4			2
#define __HWORD_WIDTH4			16

#if	 (WORD_SIZE == 8)
	/* compcert 不兼容 */
	//typedef __uint128_t		dword_t;
	
	typedef __UINT64_TYPE__		word_t;
	typedef __INT64_TYPE__		sword_t;
	typedef unsigned int		hword_t;
	typedef int			shword_t;
	
#elif   (WORD_SIZE == 4)
	// typedef uint64_t		dword_t;
	typedef unsigned long		word_t;
	typedef long			sword_t;
	typedef unsigned short		hword_t;
	typedef short			shword_t;
#else
	#error "unsupported word width"
#endif

typedef struct dword{
	word_t lword;
	word_t hword;
} dword_t;
